package com.wuhenzhizao;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorGroup;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;

/**
 * 购物车动画
 *
 * @author wuhenzhizao
 * @since 2017-08-02
 */
public class AddToCartAnimation extends AnimatorGroup {
    private static final int THREE_HUNDRED_AND_FIFTY = 350;
    private static final int SEVEN_HUNDRED = 700;
    private static final float ONE_POINT_FIVE = 1.5f;
    private static final float ZERO_POINT_TWO = 0.2f;
    private static final float ZERO_POINT_EIGHT = 0.8f;
    private final int endY;
    private final int endX;

    /**
     * 动画
     *
     * @param component
     * @param startLocation
     * @param topCartLocation
     */
    public AddToCartAnimation(Component component, int[] startLocation, int[] topCartLocation) {
        AnimatorProperty expandAnimation = new AnimatorProperty(component);
        expandAnimation.scaleX(1.0f);
        expandAnimation.scaleXBy(ONE_POINT_FIVE);
        expandAnimation.scaleY(1.0f);
        expandAnimation.scaleYBy(ONE_POINT_FIVE);
        expandAnimation.setCurveType(CurveType.ACCELERATE_DECELERATE);
        expandAnimation.setDuration(THREE_HUNDRED_AND_FIFTY);

        AnimatorProperty collapseAnimation = new AnimatorProperty(component);
        collapseAnimation.scaleX(1.0f);
        collapseAnimation.scaleXBy(ZERO_POINT_TWO);
        collapseAnimation.scaleY(1.0f);
        collapseAnimation.scaleYBy(ZERO_POINT_TWO);
        collapseAnimation.setCurveType(CurveType.ACCELERATE_DECELERATE);
        collapseAnimation.setDuration(SEVEN_HUNDRED);
        collapseAnimation.setDelay(THREE_HUNDRED_AND_FIFTY);

        /**
         * 计算位移 动画位移的X坐标 动画位移的y坐标
         */
        endX = topCartLocation[0] - startLocation[0];
        endY = topCartLocation[1] - startLocation[1];

        AnimatorProperty translateAnimationX = new AnimatorProperty(component);
        translateAnimationX.moveFromX(0);
        translateAnimationX.moveToX(endX);
        translateAnimationX.moveFromY(0);
        translateAnimationX.moveToY(0);
        translateAnimationX.setDuration(SEVEN_HUNDRED);
        translateAnimationX.setDelay(THREE_HUNDRED_AND_FIFTY);
        translateAnimationX.setLoopedCount(Animator.INFINITE);

        AnimatorProperty translateAnimationY = new AnimatorProperty(component);
        translateAnimationY.moveFromX(0);
        translateAnimationY.moveToX(0);
        translateAnimationY.moveFromY(0);
        translateAnimationY.moveToY(endY);
        translateAnimationY.setDuration(SEVEN_HUNDRED);
        translateAnimationY.setDelay(THREE_HUNDRED_AND_FIFTY);
        translateAnimationY.setLoopedCount(Animator.INFINITE);

        AnimatorProperty alphaAnimation = new AnimatorProperty(component);
        alphaAnimation.alphaFrom(1.0f);
        alphaAnimation.alpha(ZERO_POINT_EIGHT);

        translateAnimationY.setDuration(SEVEN_HUNDRED);
        translateAnimationY.setDelay(THREE_HUNDRED_AND_FIFTY);

        runSerially(expandAnimation);
        runSerially(collapseAnimation);
        runSerially(translateAnimationX);
        runSerially(translateAnimationY);
        runSerially(alphaAnimation);
    }
}
